﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Orid.Management.Common.Job;
using System.Data.SqlClient;
using Orid.Management.Statistics.Tools.Decomposition.Model;
using Orid.Management.Statistics.Tools.Decomposition.Logic;
using Orid.Data.SqlServer; 

namespace Orid.Management.Statistics.Tools.Decomposition.Data
{
    class Job:DataBase
    {
        public virtual ModelJob GetJob(Guid JobID)
        {
            return null;
        }       

        protected Orid.Management.Statistics.Tools.Decomposition.Model.ModelJob GetJob(Guid JobID, string connectString,string jobTableName )
        {
            if (JobID == null)
            {
                return null;
            }

            Orid.Management.Statistics.Tools.Decomposition.Model.ModelJob Model = new Orid.Management.Statistics.Tools.Decomposition.Model.ModelJob();
             
 
            StringBuilder sqlText = new StringBuilder();
            sqlText.Append(" SELECT [jobs].[JobID] ");
            sqlText.Append("        ,[jobs].[OrderID] ");
            sqlText.Append("        ,[jobs].[JobClassID] ");
            sqlText.Append("        ,[jobs].[CateGoryID] ");
            sqlText.Append("        ,[jobs].[DataCount] ");
            sqlText.Append("        ,[jobs].[Datas] ");
            sqlText.Append("        ,[jobs].[JobLoad] ");
            sqlText.Append("        ,[jobs].[WordCount] ");
            sqlText.Append("        ,[jobs].[CreateDateTime] ");
            sqlText.Append("        ,[jobs].[TakeDateTime] ");
            sqlText.Append("        ,[jobs].[AllowDownloadTimes] ");
            sqlText.Append("        ,[jobs].[Progress] ");
            sqlText.Append("        ,[jobs].[Property] "); //2010-06-02
            sqlText.Append(" FROM " + jobTableName + "  [Jobs]   with(NOLOCK)");//readpast
            sqlText.Append(" WHERE    [jobs].[JobID] = @JobID ");

            using (System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(connectString))
            {

                conn.Open();
                System.Data.SqlClient.SqlParameter[] SqlParameter;
                SqlParameter = new System.Data.SqlClient.SqlParameter[]
                                 {   new System.Data.SqlClient.SqlParameter("@JobID",System.Data.SqlDbType.UniqueIdentifier), 
                               
                                 };
                SqlParameter[0].Value = JobID;

                Orid.Data.SqlServer.SqlHelper.TimeOut = msTimeOut;
                SqlDataReader orderReader = Orid.Data.SqlServer.SqlHelper.ExecuteReader(conn, System.Data.CommandType.Text, sqlText.ToString(), SqlParameter);
                Model = InitSingelJobModel(orderReader);
                if (conn.State == System.Data.ConnectionState.Open)
                    conn.Close();



            }

            return Model;
        }    
    
        //protected List<ChildImage> DeSerialize(string xml)
        //{
        //    XmlSerializer<List<ChildImage>> xss = new XmlSerializer<List<ChildImage>>();
        //    List<ChildImage> imags = xss.XmlDeserialize(xml);
        //    return imags ;
        //}

        protected ChildImageDatas  DeSerialize(string xml)
        {
            Serializer< ChildImageDatas> xss = new Serializer<ChildImageDatas>();
            ChildImageDatas imags = xss.DeSerializeXml(xml);
            return imags ;
        }
   
        protected  ModelJob  InitSingelJobModel(SqlDataReader reader)
        {
            ModelJob item = new ModelJob();

            try
            {
                while (reader.Read())
                {
                   
                  
                    item.JobID = (Guid)reader["JobID"];
                    item.OrderID = (Guid)reader["OrderID"];
                    item.JobClassID = (int)reader["JobClassID"];
                    if (reader["DataCount"] != DBNull.Value)
                    {
                        item.DataCount = (int)reader["DataCount"];
                    }

                    if (reader["WordCount"] != DBNull.Value)
                    {
                        item.WordCount = (int)reader["WordCount"];
                    }

                    //2010-09-01
                    ChildImageDatas childImages = DeSerialize((string)reader["Datas"]);                  

                    foreach (Guid imageID in childImages.ChildDataID)
                    {
                        ModelJob.ImageData childData = new ModelJob.ImageData(imageID);

                        item.ChildImage.Add(childData);


                    }


                    if (reader["CreateDateTime"] != DBNull.Value)
                        item.CreateDateTime = (DateTime)reader["CreateDateTime"];
                    if (reader["TakeDateTime"] != DBNull.Value)
                        item.TakeDateTime = (DateTime)reader["TakeDateTime"];


                    item.CateGoryID = Convert.ToInt32(reader["CateGoryID"]);
                    item.JobLoad = Convert.ToInt32(reader["JobLoad"]);
                
 
                    if (reader["AllowDownloadTimes"] != DBNull.Value)
                        item.AllowDownloadTimes = (int)reader["AllowDownloadTimes"];

                  

                    if (reader["Progress"] != DBNull.Value)
                    {
                        string progressXML = reader["Progress"].ToString();                        
                        item.ListModelProgress = GetProgress(progressXML);
                       
                    }
                    if (reader["Property"] != DBNull.Value)
                    {
                        string progressXML = reader["Property"].ToString();

                        item.MProperty = GetPropertyField(progressXML);

                        //if (property != null)
                        //{
                        //    item.MProperty = GetModelProperty(property);

                           //item.ImageType = property.ImageType.ToString();                           
                           //item.ScriptType = property.ScriptType.ToString();                           
                           //item.WordDirection = property.WordDirection.ToString();                  
                           //item.WordFlag = property.WordFlag.ToString();
                           //item.WordType = property.WordType.ToString();
                           // //2010-07-27
                           // //MaRelease21版追加 对应
                           //item.MatterType =  property.MatterType.ToString();
                           //item.Warning = property.Warning;
                           // //2010-12-22 add
                           //item.IsConstraintEntry = property.IsConstraintEntry.ToString();
                           //item.UserOrder = ((Orid.Management.Component.UserOrders)property.UserOrder).ToString();
                        //}

                    }
                }

                reader.Close();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return item;
        }
    }
}
